﻿using UnityEngine;
using System.Collections;

public class Bullet : MonoBehaviour {
	
	private static Controller _playerController;
	
	[SerializeField]
	private Rigidbody[] _childBodies;
	
	private Vector3 _tmp;

	// Use this for initialization
	void Start ()
	{
		if(_playerController == null)
		{
			_playerController = GameObject.Find("Player").GetComponent<Controller>();
		}
		StartCoroutine(SuicideTimer());
		
		_tmp = new Vector3(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100));
		_childBodies[0].angularVelocity = _tmp;
		_childBodies[1].angularVelocity = _tmp;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void FixedUpdate()
	{
		rigidbody.AddForce(transform.forward * 10.0f);
	}
	
	private void OnTriggerEnter(Collider col)
	{
		if(col.tag == "enemy")
		{
			_playerController.RegisterHit(col.transform);
			Destroy(gameObject);
		}
	}
	
	private IEnumerator SuicideTimer()
	{
		yield return new WaitForSeconds(1.5f);
		_playerController.RegisterMiss();
		Destroy(gameObject);
	}
}
